/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.shopping.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itlaobing.annotation.RequestMapping;
import cn.itlaobing.shopping.exception.BusinessExceeption;
import cn.itlaobing.shopping.model.OrderModel;
import cn.itlaobing.shopping.model.UserModel;
import cn.itlaobing.shopping.service.ShopService;
import cn.itlaobing.shopping.service.UserService;
/** 
 * ClassName:ShopServlet<br/> 
 * Function: servlet类 <br/> 
 * date: 2017年11月1日 上午10:48:39 <br/> 
 * 
 * @author 王建蕊 
 * @version 1.0 
 * @since JDK 1.8 
 */
@WebServlet(value="/shopservlet/*",loadOnStartup=1)
public class ShopServlet extends BaseServlet{
	/** 
	 * doDetail:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author Administrator 
	 * @param req
	 * @param resp 
	 * @since JDK 1.8 
	 */
//	@RequestMapping("/detail")
//	public void doDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
//		req.setCharacterEncoding("utf-8");
//		ShopService ss=new ShopService();
//		List<OrderModel> list=null;
//		try {
//			list=ss.query(id);
//			System.out.println(list);
//		} catch (BusinessExceeption e) {
//			e.printStackTrace();
//		}
//		req.getSession().setAttribute("ORDER", list);
//		req.getRequestDispatcher("/WEB-INF/views/detail.jsp").forward(req, resp);
//		
//		
//	}
	@RequestMapping("/login")
	public void doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		//得到参数
		String username=req.getParameter("username");
		String userpwd=req.getParameter("userpwd");
		//调用业务类
		UserService us=new UserService();
		UserModel um=null;
		try {
			um=us.login(username, userpwd);
		} catch (BusinessExceeption e) {
			e.printStackTrace();
		}
		if(um==null) {
			//登录失败
			req.setAttribute("MSG", "用户名或密码有误！");
			req.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp);
			return;
		}else {
			//登录成功
			req.getSession().setAttribute("USER", um);
			System.out.println(um);
			//req.getRequestDispatcher("/WEB-INF/views/detail.jsp").forward(req, resp);
			ShopService ss=new ShopService();
			List<OrderModel> list=null;
			try {
				list=ss.query(um.getId());
				System.out.println(list);
			} catch (BusinessExceeption e) {
				e.printStackTrace();
			}
			req.getSession().setAttribute("ORDER", list);
			req.getRequestDispatcher("/WEB-INF/views/detail.jsp").forward(req, resp);
			
		}
	}
}
